<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: application_top.php 11 2013-09-21 11:43:11Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 11 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 11:43:11 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

define('PAGE_PARSE_START_TIME', microtime());

define('_VALID_XTC', true);

error_reporting(E_ALL & ~(E_STRICT|E_NOTICE));

if(!ini_get('display_errors'))
    ini_set('display_errors', 0);

if(function_exists('ini_set'))
	ini_set('session.use_trans_sid', 0);

if(file_exists('includes/local/configure.php') && filesize('includes/local/configure.php') !== false)
	include('includes/local/configure.php');
elseif(file_exists('includes/configure.php') && filesize('includes/configure.php') !== false)
	include('includes/configure.php');
elseif(defined('MERCARI_INSTALLED') && MERCARI_INSTALLED == false) {
	header('Location: ../installer/');
	exit;
} else {
	header('Location: ../installer/');
	exit;
}
if(MERCARI_INSTALLED != 'true') {
	header('Location: ../installer/');
	exit;
}

$request_type = (isset($_SERVER["HTTPS"]) && ($_SERVER["HTTPS"] == '1' || $_SERVER["HTTPS"] == 'on')) ? 'SSL' : 'NONSSL';

date_default_timezone_set('Europe/Berlin');

define('SQL_CACHEDIR', DIR_FS_CATALOG.'cache/');

include('includes/filenames_admin.php');
include('includes/database_admin.php');

require(DIR_FS_CATALOG.DIR_WS_CLASSES.'class.mercari_db.php');
$db = new mercari_db();

require_once(DIR_FS_INC.'inc.get_ip_address.php');
require_once(DIR_FS_INC.'inc.validate_email.php');
require_once(DIR_FS_INC.'inc.not_null.php');
require_once(DIR_FS_INC.'inc.add_tax.php');
require_once(DIR_FS_INC.'inc.get_tax_rate.php');
require_once(DIR_FS_INC.'inc.get_qty.php');
require_once(DIR_FS_INC.'inc.product_link.php');
require_once(DIR_FS_INC.'inc.mercari_version.php');
require_once(DIR_FS_INC.'inc.get_top_level_domain.php');
require_once(DIR_FS_INC.'inc.check_agent.php');

define('CURRENCY_SERVER_PRIMARY', 'oanda');
define('CURRENCY_SERVER_BACKUP', 'xe');

$configuration = $db->db_query("SELECT configuration_key AS cfgKey, configuration_value as cfgValue FROM ". TABLE_CONFIGURATION);
while(!$configuration->EOF) {
	define($configuration->fields['cfgKey'], $configuration->fields['cfgValue']);
	$configuration->MoveNext();
}

define('FILENAME_IMAGEMANIPULATOR', IMAGE_MANIPULATOR);

require(DIR_WS_FUNCTIONS.'general.php');
require(DIR_WS_FUNCTIONS.'html_output.php');

if(empty($PHP_SELF))
	$PHP_SELF = basename($_SERVER['PHP_SELF']);

require(DIR_FS_CATALOG.DIR_WS_CLASSES.'class.sessions.php');

$SESS_LIFE = (SESSION_EXPIRE_TIME*60);
$session = new sessions();

$http_domain = get_top_level_domain(defined("HTTP_SERVER") ? HTTP_SERVER : $_SERVER['HTTP_HOST']);
$https_domain = get_top_level_domain(defined("HTTPS_SERVER") ? HTTPS_SERVER : $_SERVER['HTTP_HOST']);
$current_domain = (($request_type == 'NONSSL') ? $http_domain : $https_domain);

if (function_exists('session_set_cookie_params')) {
	session_set_cookie_params(0, DIR_WS_CATALOG, (not_null(@$current_domain) ? '.'.$current_domain : ''));

} elseif (function_exists('ini_set')) {
	ini_set('session.cookie_lifetime', '0');
	ini_set('session.cookie_path', DIR_WS_CATALOG);
	ini_set('session.cookie_domain', (not_null($current_domain) ? '.'.$current_domain : ''));
}
if (isset($_POST[session_name() ])) {
	session_id($_POST[session_name() ]);
} elseif (isset($request_type) && ($request_type == 'SSL') && isset($_GET[session_name() ])) {
	session_id($_GET[session_name() ]);
}
$session_started = false;
ob_start();
if(SESSION_FORCE_COOKIE_USE == 'true') {
	setcookie('cookie_test', 'please_accept_for_session', time() + 60 * 60 * 24 * 30, DIR_WS_CATALOG, $current_domain);
	if(isset($_COOKIE['cookie_test'])) {
		$session->_sess_start();
		$session_started = true;
	}
} else {
	$session->_sess_start();
	$session_started = true;
}
if (!$session->_sess_check() && !empty($_SESSION['customer_id'])) {
	session_destroy();
	redirect(href_link(FILENAME_LOGIN));
}

get_customers_statuses();
if(!isset($_SESSION['customer_id']))
	redirect(href_link(FILENAME_LOGIN, 'r='.str_replace(DIR_WS_CATALOG, '', $_SERVER['REQUEST_URI'])));

if (!isset($_SESSION['language']) || isset($_GET['language'])) {
	include(DIR_FS_CATALOG.DIR_WS_CLASSES.'class.language.php');
	$lng = new language($_GET['language']);
	
	if(!isset($_GET['language']))
		$lng->get_browser_language();
	
	$_SESSION['language'] = $lng->language['directory'];
	$_SESSION['languages_id'] = $lng->language['id'];
	$_SESSION['language_code'] = $lng->language['code'];
}

require(DIR_FS_LANGUAGES.$_SESSION['language'].'/admin/'.$_SESSION['language'].'.php');
require(DIR_FS_LANGUAGES.$_SESSION['language'].'/admin/buttons.php');
$current_page = basename($_SERVER['SCRIPT_NAME']);

if(file_exists(DIR_FS_LANGUAGES.$_SESSION['language'].'/admin/'.$current_page))
	include(DIR_FS_LANGUAGES.$_SESSION['language'].'/admin/'.  $current_page);

require('../'.DIR_WS_INCLUDES.'write_customers_status.php');

if(file_exists($current_page) == false || $_SESSION['customers_status']['customers_status_id'] !=='0')
	redirect(href_link(FILENAME_LOGIN));

$_SESSION['user_info'] = array();
if (!isset($_SESSION['user_info']['user_ip'])) {
	$_SESSION['user_info']['user_ip'] = $_SERVER['REMOTE_ADDR'];
	$_SESSION['user_info']['user_host'] = gethostbyaddr($_SERVER['REMOTE_ADDR']);
	$_SESSION['user_info']['advertiser'] = (isset($_GET['ad']) ? $_GET['ad']: '');
	$_SESSION['user_info']['referer_url'] = $_SERVER['HTTP_REFERER'];
}

require(DIR_WS_FUNCTIONS.'localization.php');

if (isset($_GET['cPath']))
	$cPath = $_GET['cPath'];
else
	$cPath = '';

if (strlen($cPath) > 0) {
	$cPath_array = explode('_', $cPath);
	$current_category_id = $cPath_array[(sizeof($cPath_array)-1)];
} else
	$current_category_id = 0;

include(DIR_WS_CLASSES.'class.page_break.php');
include(DIR_WS_CLASSES.'class.object_info.php');
include(DIR_WS_CLASSES.'class.table_block.php');
include(DIR_WS_CLASSES.'class.upload.php');
include(DIR_WS_CLASSES.'class.box.php');
if(STORE_PAGE_PARSE_TIME == 'true')
	include(DIR_WS_CLASSES.'class.logger.php');
require(DIR_FS_CATALOG.DIR_WS_CLASSES.'class.message_stack.php');
$message_stack = new message_stack();

require(DIR_FS_CATALOG.DIR_WS_CLASSES.'Smarty_3.0.7/Smarty.class.php');

$pagename = str_replace('.php', '', basename($_SERVER['SCRIPT_NAME']));
if(!check_permission($pagename))
	redirect(href_link('error.php'));